/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package visitor;

import composite.Component;
import composite.boolean_expression.Cond;
import composite.boolean_expression.Factb;
import composite.boolean_expression.Termb;
import composite.boolean_expression.factb.FactbCond;
import composite.boolean_expression.factb.FactbNot;
import composite.boolean_expression.factb.factb_expr.RelopEq;
import composite.boolean_expression.factb.factb_expr.RelopGe;
import composite.boolean_expression.factb.factb_expr.RelopGt;
import composite.boolean_expression.factb.factb_expr.RelopLe;
import composite.boolean_expression.factb.factb_expr.RelopLt;
import composite.boolean_expression.factb.factb_expr.RelopNeq;
import composite.number_expression.Expr;
import composite.number_expression.Fact;
import composite.number_expression.Term;
import composite.number_expression.Termp;
import composite.number_expression.fact.FactExpr;
import composite.number_expression.fact.FactId;
import composite.number_expression.fact.FactNum;
import java.util.Iterator;

/**
 *
 * @author Antonio
 */
public class VisitPostfix implements Visitor {

    private StringBuilder sb;

    public VisitPostfix() {

        sb = new StringBuilder("\n\t-- STAMPA IN ORDINE POSTFISSO --\n");

    }

    @Override
    public void visit(Cond cond) {

        if (cond.getState().size() == 1) {

            cond.getState().get(0).accept(this);
            sb.append(" Cond ");
        } else {

            Iterator<Component> it = cond.getState().iterator();

            it.next().accept(this);
            while (it.hasNext()) {

                it.next().accept(this);
                sb.append(" Cond ");

            }
        }
    }

    @Override
    public void visit(Termb termb) {

        if (termb.getState().size() == 1) {

            termb.getState().get(0).accept(this);
            sb.append(" Termb ");
        } else {

            Iterator<Component> it = termb.getState().iterator();

            it.next().accept(this);
            while (it.hasNext()) {

                it.next().accept(this);
                sb.append(" Termb ");

            }
        }
    }

    @Override
    public void visit(FactbCond factbCond) {

        factbCond.getState().accept(this);
        sb.append(" FactCond ");
    }

    @Override
    public void visit(FactbNot factbNot) {

        factbNot.getState().accept(this);
        sb.append(" FactNot ");
    }

    @Override
    public void visit(RelopEq relopEq) {

        relopEq.getleft().accept(this);

        relopEq.getRight().accept(this);

        sb.append(" RelopEq ");

    }

    @Override
    public void visit(RelopGe relopGe) {

        relopGe.getleft().accept(this);

        relopGe.getRight().accept(this);

        sb.append(" RelopGe ");
    }

    @Override
    public void visit(RelopGt relopGt) {

        relopGt.getleft().accept(this);

        relopGt.getRight().accept(this);

        sb.append(" RelopGt ");
    }

    @Override
    public void visit(RelopLe relopLe) {

        relopLe.getleft().accept(this);

        relopLe.getRight().accept(this);

        sb.append(" RelopLe ");
    }

    @Override
    public void visit(RelopLt relopLt) {

        relopLt.getleft().accept(this);

        relopLt.getRight().accept(this);

        sb.append(" RelopLt ");

    }

    @Override
    public void visit(RelopNeq relopNeq) {

        relopNeq.getleft().accept(this);

        relopNeq.getRight().accept(this);

        sb.append(" RelopNeq ");
    }

    @Override
    public void visit(Expr expr) {

        if (expr.getStato().size() == 1) {

            expr.getStato().get(0).accept(this);
            sb.append(" Expr ");
        } else {

            Iterator<Component> it = expr.getStato().iterator();

            it.next().accept(this);
            while (it.hasNext()) {

                it.next().accept(this);
                sb.append(" Expr ");
            }
        }
    }

    @Override
    public void visit(Termp termp) {

        if (termp.getStato().size() == 1) {

            termp.getStato().get(0).accept(this);
            sb.append(" Termp ");
        } else {

            Iterator<Component> it = termp.getStato().iterator();

            it.next().accept(this);
            while (it.hasNext()) {

                it.next().accept(this);
                sb.append(" Termp ");
            }
        }
    }

    @Override
    public void visit(Term term) {

        if (term.getStato().size() == 1) {

            term.getStato().get(0).accept(this);
            sb.append(" Term ");
        } else {

            Iterator<Component> tp = term.getStato().iterator();

            tp.next().accept(this);
            while (tp.hasNext()) {

                tp.next().accept(this);

                sb.append(" Term ");
            }
        }
    }

    @Override
    public void visit(FactExpr factExpr) {

        factExpr.getStato().accept(this);
    }

    @Override
    public void visit(FactId factId) {

        sb.append(" FactId ");
    }

    @Override
    public void visit(FactNum factNum) {

        sb.append(" FactNum ");
    }

    @Override
    public String toString() {

        return sb.toString();
    }
}
